package com.gmail.at.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;


/**
 * The persistent class for the schedule database table.
 * 
 */
@Entity
@NamedQueries({
	@NamedQuery(name="Schedule.searchSchedule",
		query="SELECT s FROM Schedule s WHERE s.station1 = :departure "
			+ "AND s.station2 = :arrival AND s.dateOfDeparture BETWEEN :dateBegin AND :next AND s.dateOfDeparture > :now")
})
public class Schedule implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.TABLE)
	private int scheduleID;

	@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm")
	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime dateOfArrival;


	@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm")
	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime dateOfDeparture;
	
	private int version;

	//bi-directional many-to-one association to Train
	@ManyToOne
	@JoinColumn(name="TrainID")
	private Train train;

	//bi-directional many-to-one association to Station
	@ManyToOne
	@JoinColumn(name="StationOfArriaval")
	private Station station1;

	//bi-directional many-to-one association to Station
	@ManyToOne
	@JoinColumn(name="StationOfDeparture")
	private Station station2;

	//bi-directional many-to-one association to Ticket
	@OneToMany(mappedBy="schedule", cascade = CascadeType.ALL, orphanRemoval = true)
	private List<Ticket> tickets;

	public Schedule() {
	}

	public int getScheduleID() {
		return this.scheduleID;
	}

	public void setScheduleID(int scheduleID) {
		this.scheduleID = scheduleID;
	}

	public DateTime getDateOfArrival() {
		return this.dateOfArrival;
	}

	public void setDateOfArrival(DateTime dateOfArrival) {
		this.dateOfArrival = dateOfArrival;
	}

	public DateTime getDateOfDeparture() {
		return this.dateOfDeparture;
	}

	public void setDateOfDeparture(DateTime dateOfDeparture) {
		this.dateOfDeparture = dateOfDeparture;
	}

	public Train getTrain() {
		return this.train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public Station getStation1() {
		return this.station1;
	}

	public void setStation1(Station station1) {
		this.station1 = station1;
	}

	public Station getStation2() {
		return this.station2;
	}

	public void setStation2(Station station2) {
		this.station2 = station2;
	}

	@JsonIgnore
	public List<Ticket> getTickets() {
		return this.tickets;
	}

	public void setTickets(List<Ticket> tickets) {
		this.tickets = tickets;
	}

	@JsonIgnore
	@Version
	public int getVersion() {
		return version;
	}

	@Version
	public void setVersion(int version) {
		this.version = version;
	}
	
	@Transient
	private String searchDate;
	
	
	@Transient
	public String getDepartureString() {
		String departureString = "";
		if (dateOfDeparture != null)
			departureString = org.joda.time.format.DateTimeFormat.forPattern(
					"yyyy-MM-dd hh:mm").print(dateOfDeparture);
		return departureString;
	}
	
	@Transient
	public String getArrivalString() {
		String arrivalString = "";
		if (dateOfArrival != null)
			arrivalString = org.joda.time.format.DateTimeFormat.forPattern(
					"yyyy-MM-dd hh:mm").print(dateOfArrival);
		return arrivalString;
	}

	@Transient
	public String getSearchDate() {
		return searchDate;
	}

	@Transient
	public void setSearchDate(String searchDate) {
		this.searchDate = searchDate;
	}
}